package com.jvm123.blog.controller

import com.jvm123.blog.entity.Post
import com.jvm123.blog.service.MenuService
import com.jvm123.blog.service.SearchService
import org.apache.commons.lang3.StringUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping

/**
 *
 * @author yawn http://jvm123.com
 *     2019/12/1 9:23
 */
@Controller
class SearchController {

    @Autowired
    SearchService searchService
    @Autowired
    MenuService menuService

    /**
     *
     * @param t title
     * @param c content
     * @param k keywords
     * @param model
     */
    @GetMapping("s")
    String search(String t, String c, String wd, Model model) {
        List<Post> posts = searchService.search(t, c, wd)
        model.addAttribute("t", t)
        model.addAttribute("c", c)
        model.addAttribute("wd", wd)
        model.addAttribute("title", concat(t, c, wd))
        model.addAttribute("posts", posts)
        model.addAttribute("mainMenus", menuService.getMainMenu())
        model.addAttribute("topMenus", menuService.getTopMenu())
        "search"
    }

    static String concat(String ... strs) {
        StringBuffer sb = new StringBuffer()
        for (String str : strs) {
            if (StringUtils.isNotBlank(str)) {
                sb.append(str)
            }
        }
        new String(sb)
    }

}
